var util = require("../../utils/util");
var config = require("../../config");

var app = getApp()

Page({
  data: {
    my_id: 0,
    page_index: 0,
    chatsessions: [],
  },
  onLoad: function () {
    var that = this;
    that.setData({
      my_id: app.globalData.userInfo.id
    });
  },
  onShow: function () {
    var that = this;
    that.getNewChat();
  },
  //获取最新的消息列表
  getNewChat: function () {
    var that = this;
    wx.request({
      url: config.urls.weChatList,
      data: {
        my_id: app.globalData.userInfo.id,
        ps: 50,
        pn: that.data.page_index
      },
      success: function (res) {
        console.log(res);
        if (res.data.success) {
          that.setData({
            chatsessions: res.data.chat_sessions
          })
        }
        else {
          console.log("wechat list get failed.");
        }
      }
    });
  },

  onPullDownRefresh: function () {
    var that = this;
    if (that.data.activeIndex == 0) {
      that.setData({
        book_list: [],
        page_index: 0
      });
      that.getNewChat();
    }
    else {
      that.setData({
        book_list2: [],
        page_index2: 0
      });
    }
    wx.stopPullDownRefresh()
  },

  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '因书 - 借书、换书，因为书，而相识',
      path: that.route,
      success: function (res) {
        // 转发成功
        console.log(res.target)
      },
      fail: function (res) {
        // 转发失败
        console.log(res.target)
      }
    }
  },
  
  delete_chat:function(event) {
    console.log(event.currentTarget.id)
    wx.showModal({
      title: '提示',
      content: '确定删除这条记录吗？',
      success:function(res){
        if (res.confirm) {

        }
      }
    })
  }
});

var by = function (time) {
  return function (o, p) {
    var a, b;
    if (typeof o === "object" && typeof p === "object" && o && p) {
      a = o[time];
      b = p[time];
      if (a === b) {
        return 0;
      }
      if (typeof a === typeof b) {
        return a > b ? -1 : 1;
      }
      return typeof a > typeof b ? -1 : 1;
    }
    else {
      throw ("error");
    }
  }
}
